﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XNA_TD
{
    public class DrawLine : IRunable, IDraw, IUpdate
    {
        VertexPositionColor[] vertices;
        private static BasicEffect basicEffect = null;

        long startTime;

        public DrawLine(Vector3 point1, Vector3 point2)
            : base(Priority.Effekt)
        {
            vertices = new VertexPositionColor[2];
            vertices[0].Position = point1;
            vertices[0].Color = Color.White;
            vertices[1].Position = point2;
            vertices[1].Color = Color.White;

            if (basicEffect == null)
            {
                basicEffect = new BasicEffect(graphics.GraphicsDevice);
                basicEffect.VertexColorEnabled = true;
            }
        }

        public void Update()
        {
            if (startTime + 1000 < inGameTime)
            {
                UnRegister(this);
            }
            else
            {
                vertices[0].Color.B = (byte)((inGameTime - startTime) / 4);
                vertices[1].Color.B = vertices[0].Color.B;
                vertices[0].Color.G = vertices[0].Color.B;
                vertices[1].Color.G = vertices[0].Color.B;
            }
        }

        public void Draw()
        {
            basicEffect.View = viewMatrix;
            basicEffect.Projection = projectionMatrix;
            foreach (var effect in basicEffect.CurrentTechnique.Passes)
            {
                effect.Apply();
                graphics.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.LineStrip, vertices, 0, 1);
            }
        }

        public override void Load()
        {
            startTime = inGameTime;
        }

        public override void UnLoad()
        {
        }
    }
}
